package com.bootdo.system.controller;

import com.bootdo.system.domain.BizhongDO;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.domain.JiaoyiVO;
import com.bootdo.system.domain.JiaoyicelueDO;
import com.bootdo.system.service.BizhongService;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.service.JiaoyicelueService;
import com.huobi.HuobiInfoUtils;
import com.huobi.exception.SDKException;
import com.huobi.model.account.Balance;
import com.huobi.model.market.MarketTicker;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class JiaoYiController extends BaseController {

    @Autowired
    private JiaoyicelueService jiaoyicelueService;
    @Autowired
    private FrontuserService frontuserService;
    @Autowired
    private BizhongService bizhongService;

    @GetMapping("/alljiaoyishezhi")
    public String alljiaoyishezhi(Integer[] bizhongids, Model model) {

        String str = "";

        for (Integer bizhongid : bizhongids) {

            str = str + bizhongid + "-";

        }

        model.addAttribute("bizhongids", str);

        return "celueedit2";
    }

    @GetMapping("/jiaoyi/detail")
    public String detail(Long id, HttpServletRequest request) {
        //交易策略
        JiaoyicelueDO jiaoyicelueDO = jiaoyicelueService.get(id);

        //币种名字
        BizhongDO bizhongDO = bizhongService.get(jiaoyicelueDO.getBizhongid());
        request.setAttribute("bizhongDO", bizhongDO);

        if (jiaoyicelueDO.getChicangjunjia().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal subtract = bizhongDO.getNowprice().subtract(jiaoyicelueDO.getChicangjunjia());
            BigDecimal divide = subtract.divide(jiaoyicelueDO.getChicangjunjia(), 4, BigDecimal.ROUND_UP);
            jiaoyicelueDO.setZhangfu(divide);
            jiaoyicelueService.update(jiaoyicelueDO);
        }

        request.setAttribute("jiaoyicelueDO", jiaoyicelueDO);
        return "jiaoyidetail";
    }

    @GetMapping("/jiaoyi")
    public String jiaoyiPage(String keyword, Model model, HttpServletRequest request) {

        //全部循环
        List<JiaoyiVO> jiaoyiVOS = jiaoyicelueService.listwithKeyword(getUserId(request), keyword);
        model.addAttribute("jiaoyiVOS", jiaoyiVOS);
        //策略循环
        List<JiaoyiVO> celuelist = new ArrayList<>();
        //单次循环
        List<JiaoyiVO> dancilist = new ArrayList<>();
        //停止
        List<JiaoyiVO> tingzhilist = new ArrayList<>();

        for (JiaoyiVO jiaoyi : jiaoyiVOS) {
            //循环的开启的
            if (jiaoyi.getXunhuantype() == 1&&jiaoyi.getCeluestatus()==1) {
                celuelist.add(jiaoyi);
            }
            //单次的开启的
            if(jiaoyi.getXunhuantype() == 0&&jiaoyi.getCeluestatus()==1) {
                dancilist.add(jiaoyi);
            }
            //停止的
            if (jiaoyi.getCeluestatus() == 0) {
                tingzhilist.add(jiaoyi);
            }
        }

        model.addAttribute("celuelist", celuelist);
        model.addAttribute("dancilist", dancilist);
        model.addAttribute("tingzhilist", tingzhilist);

        FrontuserDO userDO = getUserDO(request);
        model.addAttribute("userDO", userDO);
        model.addAttribute("accountyue", 0);

        try {
            if (StringUtils.isNotEmpty(userDO.getHuobiapikey()) && StringUtils.isNotEmpty(userDO.getHuobiapisecret())) {
                Balance accountyue = HuobiInfoUtils.getAccountyue(userDO.getHuobiapikey(), userDO.getHuobiapisecret());
                model.addAttribute("accountyue", accountyue.getBalance().stripTrailingZeros().toPlainString());
            }
        } catch (SDKException e) {
            System.out.println("交易页面数据获取失败：" + e);
            model.addAttribute("accountyue", "0");
        }


        model.addAttribute("active", "2");
        return "jiaoyi";
    }
}
